package com.gujiangbo.application.proxy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gujiangbo.application.common.base.SystemConstant;
import com.gujiangbo.application.common.fc.FlowControlManage;
import com.gujiangbo.application.common.trace.TraceManager;
import com.gujiangbo.application.config.CommonZKConfig;
import com.gujiangbo.application.log.HttpInfoBillBuilder;
import com.gujiangbo.application.log.service.HttpInfoBillService;
import com.gujiangbo.application.log.support.HttpInfoBillEvt;
import com.gujiangbo.application.utils.BeanTools;
import com.gujiangbo.application.utils.CommonTools;
import com.gujiangbo.application.utils.ContextHolder;
import com.gujiangbo.application.utils.IDTools;
import com.gujiangbo.application.utils.InternetProtocolTool;
import com.gujiangbo.application.utils.SafeIdentificationCheck;

@Aspect
@Component
public class HttpNorthProxy {
	@Autowired
	private SystemConstant systemConstant;
	@Autowired
	private FlowControlManage flowControlManage;
	@Autowired
	private CommonZKConfig commonzkConfig;

	@Pointcut("execution(* com.gujiangbo.application.manage.service.impl.HttpNorthManageServiceImpl.*(..))")
	private void anyMethod() {
	}

	@Around("anyMethod()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		HttpInfoBillService service = (HttpInfoBillService) ContextHolder
				.getService(pjp.getSignature().getName() + "LogInfBillService");
		if (service == null) {
			service = (HttpInfoBillService) ContextHolder.getService("paymentNorthLogInfBillService");
		}
		String traceID = "";
		if (StringUtils.isBlank(traceID)) {
			traceID = "TRACEID" + IDTools.getID();
			TraceManager.setTraceID(traceID);
		}
		HttpInfoBillEvt logInfBillEvt = service.packageRequestData(pjp.getArgs());
		if ((pjp.getArgs() != null) && (pjp.getArgs().length > 1)
				&& ((pjp.getArgs()[1] instanceof HttpServletRequest))) {
			try {
				HttpServletRequest request = (HttpServletRequest) pjp.getArgs()[1];
				Map<String, String> headerMap = SafeIdentificationCheck.getInstantce().getHeaderMap(request);
				String ip = InternetProtocolTool.getRemoteAddr(request);
				headerMap.put("IP", ip);
				logInfBillEvt.setReqHeader(BeanTools.beanObjectToJsonStr(headerMap));

				String traceid = (String) headerMap.get("traceid");
				if (StringUtils.isNotBlank(traceid)) {
					TraceManager.setTraceID(traceid);
				}
			} catch (Exception localException) {
			}
		}
		logInfBillEvt.setInfname(pjp.getSignature().getName());
		logInfBillEvt.setFlowDirect("1");
		Date startTime = new Date();
		logInfBillEvt.setReceiveTime(sf.format(startTime));
		Object returnObj = null;
		String[] methodName = { "setReturnCode", "setReturnMsg" };
		if ("1".equals(this.systemConstant.getSwitch_pay())) {
			String not_support_open_methods =

					this.commonzkConfig.getNot_support_open_methods();
			if (StringUtils.isBlank(not_support_open_methods)) {
				not_support_open_methods = "";
			}
			if ("1".equals(this.commonzkConfig.getOpen_switch_pay())) {
				if (!not_support_open_methods.contains("," + pjp.getSignature().getName() + ",")) {
					returnObj = CommonTools.getOpenRspObj(methodName, pjp);
				}
			}
			if (!this.flowControlManage.getSignPoint(pjp.getSignature().getName())) {
				returnObj = CommonTools.getSoapReturnObj(methodName, pjp, "max");
			} else {
				returnObj = pjp.proceed();
			}
		} else {
			returnObj = CommonTools.getReturnObj(methodName, pjp);
		}
		logInfBillEvt = service.packageResponseData(returnObj, logInfBillEvt);
		Date endTime = new Date();
		logInfBillEvt.setResponseTime(sf.format(endTime));
		logInfBillEvt.setUseTime(String.valueOf(endTime.getTime() - startTime.getTime()));
		logInfBillEvt.setTraceid(TraceManager.getTraceID());
		HttpInfoBillBuilder.getInstance().billBulder(logInfBillEvt);

		return returnObj;
	}

}
